<?php

namespace app\beautiful\model;

use app\common\model\CommonModel;

use app\beautiful\model\member\MemberModel;
use app\beautiful\model\setting\PartnerDividendSettingModel;
use think\facade\Db;


class PartnerModel extends CommonModel{

    protected  $name = 'beautiful_partner';

    protected  $pk   = 'partner_id';





    public function getIsExpireAttr($value,$data)

    {

        return $data['expire_time'] ? ($data['expire_time'] >= date("Y-m-d") ? 0 : 1 ) : 1;

    }
    public static function list($where,$order){
        $query = Db::name('beautiful_partner')->alias('a')
            ->join('shop b','a.shop_id=b.shop_id')
            ->join('shop_detail d','a.shop_id=d.shop_id')
            ->where(['a.status'=>1,'a.is_delete'=>0])
            ->whereIn('a.role_id',[2,5,6])
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->field("a.*,b.name as shop_name,d.logo,d.tel");
        $list=$query->order($order)
            ->select()
            ->toArray();
//        var_dump(Db::getLastSql());exit();
        return $list;
    }
    public static function plist($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_partner')->alias('a')
            ->where(['a.status'=>1,'a.is_delete'=>0])
            ->where([['a.member_id','>',0]])
            ->whereIn('a.role_id',[2,5,6])
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['partner_id']),function ($query) use ($where){
                $query->where('a.partner_id','>',$where['partner_id']);
            })
            ->field("a.*");
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
//        var_dump(Db::getLastSql());exit();
        return $list;
    }
    public static function info($where,$order=['a.partner_id'=>'desc']){
        $query = Db::name('beautiful_partner')->alias('a')
            ->join('shop b','a.shop_id=b.shop_id')
            ->join('beautiful_member c','a.member_id=c.member_id')
            ->join('shop_detail d','a.shop_id=d.shop_id')
            ->where(['a.status'=>1,'a.is_delete'=>0])
            ->whereIn('a.role_id',[2,5,6])
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['partner_id']),function ($query) use ($where){
                $query->where(['a.partner_id'=>$where['partner_id']]);
            })
            ->field("a.*,b.name as shop_name,d.logo,d.tel,c.member_num,c.face");

        $info=$query->order($order)
            ->find();
        if(!empty($info)){
            $info['add_time']=date('Y-m-d',$info['add_time']);
            $ratio=self::ratio($info);
            $info['money']=$ratio['money'];
            $info['ratio']=$ratio['ratio'];
            $info['buy_ratio']=$ratio['buy_ratio'];
        }
//        var_dump(Db::getLastSql());exit();
        return $info;
    }
    public static function ratio($list){
        $ratio=0;
        $buy_ratio=0;
        $money=0;
        $s=[];
        $s['role_id']=$list['role_id'];
        $s['shop_id']=$list['shop_id'];
        $s['is_delete']=0;
        $setting=PartnerDividendSettingModel::where($s)->find();
        if(!empty($setting)){
            $range=json_decode($setting['range'],true);
            if($list['role_id']==2){
                if($setting['staff_type']==1){
                    $money=$list['invest_amount'];
                }elseif ($setting['staff_type']==2){
                    $money=$list['stored_value_amount'];
                }
            }elseif($list['role_id']==5){
                $money=$list['invest_amount'];
            }elseif($list['role_id']==6){
                $money=$list['stored_value_amount'];
            }
            foreach ($range as $r_v){
                if(!empty($r_v['end'])){
                    if($money>=$r_v['start'] && $money<=$r_v['end']){
                        $ratio=$r_v['ratio'];
                        $buy_ratio=empty($r_v['buy_ratio'])?0:$r_v['buy_ratio'];
                    }
                }else{
                    if($money>=$r_v['start']){
                        $ratio=$r_v['ratio'];
                        $buy_ratio=empty($r_v['buy_ratio'])?0:$r_v['buy_ratio'];
                    }
                }
            }
        }
        return compact('ratio','money','buy_ratio');
    }


    public function member(){

        return  $this->hasOne(MemberModel::class,'member_id','member_id');

    }


    public function getRoleMeansAttr($value,$data)

    {

        $role_means=[

            1=>'股金合伙人',

            2=>'员工合伙人',

            3=>'会员合伙人',

            4=>'自定义合伙人'

        ];

        return $role_means[$data['role_id']] ?? '-';

    }

}

